import App from './App'
import store from 'store/store.js'
// 导入请求需求包
import {
	$http
} from '@escook/request-miniprogram'
// 把包挂载到全局属性上
uni.$http = $http
// 配置请求根路径
$http.baseUrl = 'https://test-shopping-back.bluej.cn'

// 需要token的名单
const whiteList = [
	'/api/shoppingCart',
	'/api/address',
	'/api/setDefault',
	'/api/user',
	'/api/order'
]
// 配置请求拦截器 和 响应拦截器
$http.beforeRequest = function(options) {
	// do somethimg... 添加token请求头
	let Flower = uni.getStorageSync('Flower')
	let token = Flower ? Flower.userInfo.token : ''
	let pass;
	whiteList.forEach(item => {
		let url = uni.$http.baseUrl + item
		if (options.url.includes(url)) {
			pass = true
		}
	})
	if (pass) {
		options.header = {
			'x-token': token
		}
	}
}
$http.afterRequest = function() {
	// do something... 统一配置响应拦截操作
	// console.log(response)
}

// #ifndef VUE3
import Vue from 'vue'
Vue.config.productionTip = false
App.mpType = 'app'
// 引用并使用JS库
import uView from "uview-ui";
Vue.use(uView);
// 全局引入tarBar
import tabBar from 'components/TabBar.vue';
Vue.component('tab-bar', tabBar)

try {
	function isPromise(obj) {
		return (
			!!obj &&
			(typeof obj === "object" || typeof obj === "function") &&
			typeof obj.then === "function"
		);
	}

	// 统一 vue2 API Promise 化返回格式与 vue3 保持一致
	uni.addInterceptor({
		returnValue(res) {
			if (!isPromise(res)) {
				return res;
			}
			return new Promise((resolve, reject) => {
				res.then((res) => {
					if (res[0]) {
						reject(res[0]);
					} else {
						resolve(res[1]);
					}
				});
			});
		},
	});
} catch (error) {}

const app = new Vue({
	...App,
	store
})

app.$mount()
// #endif

// #ifdef VUE3
import {
	createSSRApp
} from 'vue'
export function createApp() {
	const app = createSSRApp(App)
	return {
		app
	}
}
// #endif